package cn.hfatec.shms.system.keyGenerator.service;

import cn.hfatec.JsonResponse;
import cn.hfatec.Page;
import cn.hfatec.Table;
import cn.hfatec.base.BaseDao;
import cn.hfatec.shms.system.keyGenerator.dao.KeyGeneratorDao;
import cn.hfatec.shms.system.keyGenerator.model.KeyGenerator;
import cn.hfatec.shms.system.keyGenerator.model.KeyGeneratorParams;
import com.google.common.base.Optional;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;



/**
 *
 * @author hfatec
 * @version 1.0
 *
 * the JAVA code is generate by middlegen
 *
 */
@Service("KeyGeneratorService")
public class KeyGeneratorService {

    public static final String BEAN_ID = "KeyGeneratorService";

    //注入Dao层
    @Resource(name = "KeyGeneratorDao")
    private KeyGeneratorDao keyGeneratorDao;

    //注入BaseDao
    @Resource(name = "BaseDao")
    private BaseDao baseDao;

    /**
     *
     * 查询数据
     *
     * @param keyGeneratorParams
     * @param page
     *
     */
    @Transactional
    public Table queryKeyGeneratorTable(KeyGeneratorParams keyGeneratorParams, Page page){

        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("params", keyGeneratorParams);
        return baseDao.queryTableByPage("queryKeyGenerator", params, page);
    }

    /**
     *
     * 插入数据
     * @param keyGenerator
     *
     */
    @Transactional
    public void saveKeyGenerator(KeyGenerator keyGenerator) {
        if(keyGenerator.getId() != null) {
            KeyGenerator result = getKeyGenerator(keyGenerator.getId());
            if(result != null) {
                keyGeneratorDao.update(keyGenerator);
            } else {
                keyGeneratorDao.insert(keyGenerator);
            }
        } else {
            keyGeneratorDao.insert(keyGenerator);
        }
    }

    /**
     *
     * 检查自增主键类型是否重复
     *
     * @param keyGenerator
     * @return
     */
    public Optional<JsonResponse<String>> checkRepeat(KeyGenerator keyGenerator) {
        KeyGeneratorParams params = new KeyGeneratorParams();
        params.setType(keyGenerator.getType());
        List list = keyGeneratorDao.queryKeyGenerator(params);
        if(list.size() > 0) {
            return Optional.of(new JsonResponse<String>(JsonResponse.RETURN_FAILURE, "自增主键类型重复，不能新增"));
        }
        return Optional.absent();
    }


    /**
     *
     * 删除数据
     * @param id
     *
     */
    @Transactional
    public void deleteKeyGenerator(Integer id) {

        keyGeneratorDao.delete(id);
    }

    /**
     *
     * 删除数据
     * @param keyGeneratorList
     *
     */
    @Transactional
    public void deleteKeyGeneratorBatch(List<String> keyGeneratorList) {
        for(String id : keyGeneratorList) {
            deleteKeyGenerator(Integer.valueOf(id));
        }
    }

    /**
     *
     * 根据ID获取数据
     * @param id
     *
     */
    @Transactional
    public KeyGenerator getKeyGenerator(Integer id) {

        return keyGeneratorDao.getKeyGeneratorById(id);
    }
}